package com.dahuyou.netty.nio.methods;

import org.junit.Test;

import java.nio.IntBuffer;

public class MethodsTest {

    @Test
    public void allocateTest() {
        IntBuffer intBuffer = IntBuffer.allocate(20);
        System.out.println("capacity: " + intBuffer.capacity());
        System.out.println("position: " + intBuffer.position());
        System.out.println("limit: " + intBuffer.limit());
    }

    @Test
    public void putTest() {
        IntBuffer intBuffer = IntBuffer.allocate(20);

        intBuffer.put(1);
        intBuffer.put(2);

        System.out.println("capacity: " + intBuffer.capacity());
        System.out.println("position: " + intBuffer.position());
        System.out.println("limit: " + intBuffer.limit());
    }


    @Test
    public void flipTest() {
        IntBuffer intBuffer = IntBuffer.allocate(20);

        intBuffer.put(1);
        intBuffer.put(2);

        System.out.println("capacity: " + intBuffer.capacity());
        System.out.println("position: " + intBuffer.position());
        System.out.println("limit: " + intBuffer.limit());

        System.out.println("flip后");
        intBuffer.flip();
        System.out.println("capacity: " + intBuffer.capacity());
        System.out.println("position: " + intBuffer.position());
        System.out.println("limit: " + intBuffer.limit());
    }

    @Test
    public void getTest() {
        IntBuffer intBuffer = IntBuffer.allocate(20);

        intBuffer.put(1);
        intBuffer.put(2);

        System.out.println("capacity: " + intBuffer.capacity());
        System.out.println("position: " + intBuffer.position());
        System.out.println("limit: " + intBuffer.limit());

        System.out.println("flip后");
        intBuffer.flip();
        System.out.println("capacity: " + intBuffer.capacity());
        System.out.println("position: " + intBuffer.position());
        System.out.println("limit: " + intBuffer.limit());


        System.out.println("get一次后");
        System.out.println(intBuffer.get());

        System.out.println("capacity: " + intBuffer.capacity());
        System.out.println("position: " + intBuffer.position());
        System.out.println("limit: " + intBuffer.limit());

    }

    @Test
    public void rewindTest() {
        IntBuffer intBuffer = IntBuffer.allocate(20);

        intBuffer.put(1);
        intBuffer.put(2);

//        System.out.println("capacity: " + intBuffer.capacity());
//        System.out.println("position: " + intBuffer.position());
//        System.out.println("limit: " + intBuffer.limit());

//        System.out.println("flip后");
        intBuffer.flip();
//        System.out.println("capacity: " + intBuffer.capacity());
//        System.out.println("position: " + intBuffer.position());
//        System.out.println("limit: " + intBuffer.limit());


//        System.out.println("get一次后");
        System.out.println(intBuffer.get());

        System.out.println("capacity: " + intBuffer.capacity());
        System.out.println("position: " + intBuffer.position());
        System.out.println("limit: " + intBuffer.limit());

        System.out.println("rewind后");
        intBuffer.rewind();
        System.out.println("capacity: " + intBuffer.capacity());
        System.out.println("position: " + intBuffer.position());
        System.out.println("limit: " + intBuffer.limit());
        System.out.println(intBuffer.get());

    }


    @Test
    public void markAndResetTest() {
        IntBuffer intBuffer = IntBuffer.allocate(20);

        intBuffer.put(1);
        intBuffer.put(2);

        System.out.println("记录position位置2到mark");
        // 记录position位置2到mark
        intBuffer.mark();
        System.out.println("position变为3");
        // position变为3
        intBuffer.put(3);
        System.out.println("position: " + intBuffer.position());

        System.out.println("reset mark恢复position为2");
        // reset mark恢复position为2
        intBuffer.reset();
        System.out.println("position: " + intBuffer.position());


        intBuffer.compact();
    }


    @Test
    public void compactTest() {
        IntBuffer intBuffer = IntBuffer.allocate(20);

        intBuffer.put(1);
        intBuffer.put(2);
        System.out.println("写俩数据后：");
        System.out.println("capacity: " + intBuffer.capacity());
        System.out.println("position: " + intBuffer.position());
        System.out.println("limit: " + intBuffer.limit());

        intBuffer.flip(); // 转到读取模式
        System.out.println("flip后：");
        System.out.println("capacity: " + intBuffer.capacity());
        System.out.println("position: " + intBuffer.position());
        System.out.println("limit: " + intBuffer.limit());
//        System.out.println(intBuffer.get());
        // 读一个 1
        System.out.println(intBuffer.get());

        intBuffer.compact();
        System.out.println("compact后：");
        System.out.println("capacity: " + intBuffer.capacity());
        System.out.println("position: " + intBuffer.position());
        System.out.println("limit: " + intBuffer.limit());

        //
        intBuffer.flip();
        System.out.println(intBuffer.get());
    }
}
